package com.example.jetpackcomposelazycolumn.ui.screens

import androidx.compose.foundation.Image
import androidx.compose.foundation.gestures.detectTransformGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.IntOffset
import com.example.jetpackcomposelazycolumn.R
import com.example.jetpackcomposelazycolumn.models.Post
import com.google.accompanist.coil.CoilImage
import kotlin.math.PI
import kotlin.math.roundToInt


@Composable
fun ImageDetail(post: Post) {
    
    ImagePreview(link = post.preview)
}

@Composable
fun ImagePreview(link: String) {
    Box(modifier = Modifier.fillMaxSize()) {
        var angle by remember { mutableStateOf(0f) }
        var zoom by remember { mutableStateOf(1f) }
        var offsetX by remember { mutableStateOf(0f) }
        var offsetY by remember { mutableStateOf(0f) }

        CoilImage(
            data = link,
            contentDescription = "image",
            contentScale = ContentScale.Fit,
            modifier = Modifier
                .offset { IntOffset(offsetX.roundToInt(), offsetY.roundToInt()) }
                .graphicsLayer(
                    scaleX = zoom,
                    scaleY = zoom,
                    rotationZ = angle
                )
                .pointerInput(Unit) {
                    detectTransformGestures(
                        onGesture = { _, pan, gestureZoom, gestureRotate ->
                            angle += gestureRotate
                            zoom *= gestureZoom
                            val x = pan.x * zoom
                            val y = pan.y * zoom
                            val angleRad = angle * PI / 180.0
                            offsetX += (x * kotlin.math.cos(angleRad) - y * kotlin.math.sin(angleRad)).toFloat()
                            offsetY += (x * kotlin.math.sin(angleRad) + y * kotlin.math.cos(angleRad)).toFloat()
                        }
                    )
                }
                .fillMaxSize(),
            loading = {
                Image(
                    painter = painterResource(id = R.drawable.ic_photo),
                    contentScale = ContentScale.FillBounds,
                    contentDescription = null,
                    alpha = 0.45f
                )
            },
            error = {
                Image(
                    painter = painterResource(id = R.drawable.ic_broken_image),
                    contentScale = ContentScale.FillBounds,
                    contentDescription = null,
                    alpha = 0.45f
                )
            }
        )
    }
}